<?php
/**
 * 红包
 */
namespace Model;
defined('IN_IA') or exit('Access Denied');
class RedMod extends Mod
{
    public function __construct()
    {
        $this->_table = 'kuyuan_red_red';
    }

    /**
     * 获取首页红包列表
     * @param integer $_uid
     * @param integer $_enter_red_id
     * @param integer $_uniacid 公众号id
     * @param array $_limit 请求下标
     * @return mixed 返回列表
     */
    public function openRedList($_uid,$_enter_red_id,$_uniacid,$_limit)
    {
        $_sql = "
                 SELECT 
                        a.id,a.red_type,a.area_type,a.area
                   FROM
                        ".tablename($this->_table)." AS a
                   WHERE
                        a.uniacid = :uniacid
                   AND
                        a.id != :red_id
                   AND
                       (
                            a.status = :status
                       OR   
                            (SELECT id FROM ".tablename('kuyuan_red_record')." WHERE red_id = a.id AND uid = :uid AND a.status != 9)
                       )
                   AND
                        a.share = :share
                   ORDER BY
                        (SELECT id FROM ".tablename('kuyuan_red_record')." WHERE red_id = a.id AND uid = :uid) ASC,a.id desc
                   LIMIT
                        ".$_limit[0].",".$_limit[1]."  
                 ";
        return pdo_fetchall($_sql,array('uniacid'=>$_uniacid,'red_id'=>$_enter_red_id,'status'=>2,'share'=>1,'uid'=>$_uid));
    }


    /**
     * 验证指定媒体素材是否正在使用
     * @param string $_media  素材地址
     * @param integer $_uid 当前用户id
     * @param integer $_uniacid 公众号id
     * @return mixed
     */
    public function testRedUseMedia($_media,$_uid,$_uniacid)
    {
        $_sql = "
                 SELECT 
                        id
                  FROM
                        ".tablename($this->_table)."
                  WHERE
                        uniacid = :uniacid
                  AND
                        uid = :uid
                  AND
                        status != 9
                  AND
                        status != 1
                  AND
                        (
                            cover = :media
                        OR
                            media = :media          
                        ) 
                  LIMIT
                        1
                ";
        return pdo_fetch($_sql,array('uniacid'=>$_uniacid,'uid'=>$_uid,'media'=>$_media));
    }

    /**
     * web端获取指定日期得红包金额统计数据
     * @param int $_time 开始时间
     * @param int $_end_time 结束时间
     * @param int $_uniacid 公众号id
     * @return float 金额
     */
    public function redInfo($_time,$_end_time,$_uniacid)
    {
        $_sql = "
                  SELECT 
                        SUM(`money` * `number`)
                  FROM
                        ".tablename($this->_table)."
                  WHERE
                        uniacid = :uniacid
                  AND
                        type = 2
                  AND
                        time > :start_time
                  AND
                        time <= :end_time
                  AND
                        status > 1
                ";
        $_sum = pdo_fetchcolumn($_sql,array("uniacid"=>$_uniacid,"start_time"=>$_time,"end_time"=>$_end_time));
        return $_sum ? $_sum : '0.00';
    }
}
